export default {
  data () {
    return {
      // 预警值设置
      markLine: []
    }
  },
  mounted () {
    const Echarts = require('echarts')
    this.charts = Echarts.init(this.$el, null, { locale: 'ZH' })
    this.charts.on('click', params => {
      // 关闭tip提示
      this.charts.dispatchAction({ type: 'hideTip' })
      this.$emit('showRemarkDailog', params.data)
    })
    this.charts.on('mouseout', params => {
      // 关闭tip提示
      this.charts.dispatchAction({ type: 'hideTip' })
    })
    window.addEventListener('resize', this.echartResize)
  },
  beforeDestroy () {
    window.removeEventListener('resize', this.echartResize)
  },
  methods: {
    // 设置预警值
    setWarningLine (list) {
      this.markLine = list.map(x => {
        return {
          name: 'markLine',
          yAxis: x.threshold,
          label: { normal: { show: true, position: 'start', formatter: x.warningLevelName } } 
        }
      })
      this.charts.setOption(this.options, true)
    },
    // echart随着窗口改变
    echartResize () {
      this.charts.resize()
    },
    // 加入换行
    sliceStr (val, length = 12) {
      let strs = val.split('') // 字符串数组
      let str = ''
      // eslint-disable-next-line no-cond-assign
      for (let i = 0, s; s = strs[i++];) { // 遍历字符串数组
        str += s
        if (!(i % 12)) str += '<br/>' // 按需要求余
      }
      return str
    },
    // 数据格式化转换
    formatParams (row, key) {
      return {
        value: row[key], 
        dataId: row.id, 
        remark: row.remark, 
        _time: row.createTime
      }
    }
  }
}